/*
 * @Author: szx
 * @Date: 2022-03-05 14:31:13
 * @LastEditTime: 2022-03-05 14:35:29
 * @Description:
 * @FilePath: \leetcode\100-199\105\105_2.js
 */
function TreeNode(val, left, right) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
}

/**
 * @param {number[]} preorder
 * @param {number[]} inorder
 * @return {TreeNode}
 */
var buildTree = function (preorder, inorder) {
    if (!preorder.length) return null;
    const rootVal = preorder.shift();
    const root = new TreeNode(rootVal);
    const index = inorder.indexOf(rootVal);
    root.left = buildTree(preorder.slice(0, index), inorder.slice(0, index));
    root.right = buildTree(preorder.slice(index), inorder.slice(index + 1));
    return root;
};
